//
//  BaseCollectionAdapter.swift
//  TXiOSDemoApp
//
//  Created by xicheng on 16/11/26.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

public class BaseCollectionViewAdapter<MODEL:Any,CELL:UICollectionViewCell,TARGET_VC:TXBaseViewController>:NSObject,UICollectionViewDelegate,UICollectionViewDataSource {
  internal weak var collectionView: UICollectionView? = nil
  internal var dataList: [MODEL]? = nil
  internal var cellIdentifier: String! = nil
  internal weak var targetVC:TARGET_VC? = nil
  
  internal var isEmpty: Bool {
    get {
      return dataList == nil || dataList!.count == 0
    }
  }
  
  
  
  
  public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: self.cellIdentifier, for: indexPath) as! CELL
    self.setupCell(cell: cell, model: dataList![indexPath.row], indexPath: indexPath)
    return cell
  }
  
  
  public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return dataList == nil ? 0 : dataList!.count
  }
  
  
  func setupCell(cell: CELL, model: MODEL, indexPath: IndexPath) {
    
  }
  
  
  public func setupColletionView(_ _colletionView: UICollectionView){
    self.collectionView = _colletionView
    self.cellIdentifier = "\(tx_className())_\(TagIdGenerator.sharedInstance.generatorTagId())"
    self.collectionView?.delegate = self
    self.collectionView?.dataSource = self
    self.collectionView?.register(CELL.self, forCellWithReuseIdentifier: cellIdentifier)
    
  }
  
  func refreshCount(listData: [MODEL]?) {
    self.dataList?.removeAll()
    self.dataList = listData
    collectionView?.reloadData()
  }
  
  
}
